<?php
require("tokenizer.php");
require("access/connect.php");

	function getLinks($fileName){
			$text = file2string($fileName);	
			$text = convert2utf($text);
			$urls = extract_html_urls($text);
			
			$a = $urls['a']['href'];
	return $a;
}

	function getLinkGraph($urls, $filename){
		foreach($urls as $link){
				$file = "articles/".$link;
				$res = mysql_query("SELECT CID FROM articles_table WHERE Filename = '$file'")or die("Could not get articles ID".mysql_error());
				$row = mysql_fetch_row($res);
				$CID[] = $row[0];
			}
			
		if(!empty($row)){
			$linkstr= implode("-", $CID);//creating string
			mysql_query("UPDATE articles_table SET links = '$linkstr' WHERE Filename = '$filenane'")or die("Links could not be loaded. ".mysql_error());
		}
		 $res = mysql_query("SELECT CID, links FROM articles_table")or die("Query Failed".mysql_error());
		 
		 while($row = mysql_fetch_row($res)){
			$arr = explode("-", $row[1]);
			$contID = $row[0];
			$linkGraph[$contID] = $arr;	 
		 }
		return $linkGraph;
	}
	
	function calculatePageRank($linkGraph, $dampingFactor = 0.15) {
        $pageRank = array();
        $tempRank = array();
        $nodeCount = count($linkGraph); 

        // initialise the PR as 1/n
        foreach($linkGraph as $node => $outbound) {
                $pageRank[$node] = 1/$nodeCount;
                $tempRank[$node] = 0;
        }

        $change = 1;
        $i = 0;
        while($change > 0.00005 && $i < 100) {
                $change = 0;
                $i++;

                // distribute the PR of each page
                foreach($linkGraph as $node => $outbound) {
                        $outboundCount = count($outbound);
                        foreach($outbound as $link) { 
                                $tempRank[$link] += $pageRank[$node] / $outboundCount;
                        }
                }
                
                $total = 0;
                // calculate the new PR using the damping factor
                foreach($linkGraph as $node => $outbound) {
                        $tempRank[$node]  = ($dampingFactor / $nodeCount)
                                                + (1-$dampingFactor) * $tempRank[$node];
                        $change += abs($pageRank[$node] - $tempRank[$node]);
                        $pageRank[$node] = $tempRank[$node];
                        $tempRank[$node] = 0;
                        $total += $pageRank[$node];
                }

                // Normalise the page ranks so it's all a proportion 0-1
                foreach($pageRank as $node => $score) {
                        $pageRank[$node] /= $total;
                }
        }
        
        return $pageRank;
	}

	$urls = getLinks('articles/computer.html');
	
	$linkGraph = getLinkGraph($urls);
	
	print_r($pageRank = calculatePageRank($linkGraph));
	
	foreach($pageRank as $key =>$rank){
					$pr = floatval($rank);
					$k = intval($key);
					mysql_query("UPDATE articles_table SET pagerank = $pr WHERE CID = $k")or die("PageRank could not be updated. ".mysql_error());
				
				}
	
?>